package com.basic.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: wangzhi
 * @create: 2023-12-29 11:37
 **/
@RestController
@RequestMapping
public class LoginController {

    @GetMapping(value = "/login")
    public ModelAndView login(){
        ModelAndView view = new ModelAndView();
        view.setViewName("login");
        return view;
    }

    @GetMapping(value = {"/","/home"})
    public ModelAndView home(){
        ModelAndView view = new ModelAndView();
        view.setViewName("home");
        return view;
    }

    @GetMapping(value = "/hello")
    public ModelAndView hello(){
        ModelAndView view = new ModelAndView();
        view.setViewName("hello");
        return view;
    }

    @GetMapping("/info")
    public String info(){
        String userDetails = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails) {
            userDetails = ((UserDetails)principal).getUsername();
        }else {
            userDetails = principal.toString();
        }
        return userDetails;
    }
































    /**
     * map集合模糊获取
     * @param map
     * @param keyLike
     * @return
     */
    public static <T> List<T> getLikeByMap(Map<String, T> map, String keyLike){
        List<T> list = new ArrayList<>();
        if (map.size() > 0){
            list.add(map.entrySet().stream()
                    .filter((e) -> e.getKey().contains(keyLike))
                    .findFirst().get().getValue());
        }
        return list;
    }

    public static void main(String[] args) {
        UserInfo user1 = new UserInfo("zhangsan", 12);
        UserInfo user2 = new UserInfo("lisi", 12);
        UserInfo user3 = new UserInfo("wanger", 12);
        Map<String,UserInfo> mapInfo = new HashMap<>();
        mapInfo.put("zhangsan",user1);
        mapInfo.put("lisi",user2);
        mapInfo.put("wanger",user3);
        List<UserInfo> byMap = getLikeByMap(mapInfo, "s");
        System.out.println(byMap);

        Map<String,String> mapString = new HashMap<>();
        mapString.put("zhangsan","zhangsan");
        mapString.put("lisi","lisi");
        mapString.put("wanger","wanger");
        List<String> byMap1 = getLikeByMap(mapString, "s");
        System.out.println(byMap1);

    }

}
